package com.lancoo.znbkxx.teacher

import com.lancoo.znbkxx.teacher.model.data.BaseInfo
import com.lancoo.znbkxx.teacher.model.data.CoursewareOBSInfo
import com.lancoo.znbkxx.teacher.model.data.OBSInfo
import com.lancoo.znbkxx.teacher.model.data.ZnbkResponse
import com.lancoo.znbkxx.teacher.utils.MkUtils
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope

/**
 * @author : Admin
 * @date   : 2019/7/11 17:43
 * @desc   :
 */

class Ext {
    companion object {

        //        @JvmField
        var BASE_ADDRESS: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("BASE_ADDRESS") ?: ""
                }
                return field
            }

        //        @JvmField
        var PLATFORM_URL: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("PLATFORM_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var BASE_URL: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("BASE_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var BASE_URL_COMMENT: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("BASE_URL_COMMENT") ?: ""
                }
                return field
            }

        //        @JvmField
        var ONEONONE_URL = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("ONEONONE_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var WORDS_URL = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("WORDS_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var RZZD_URL = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("RZZD_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var SCHOOLPAPER_URL = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("SCHOOLPAPER_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var ANALYSIS_URL: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("ANALYSIS_URL") ?: ""
                }
                return field
            }

        //        @JvmField  这个好像没有被使用
        var PREDICTION_URL: String = ""


        //        @JvmField
        var COMMENT_URL: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("COMMENT_URL") ?: ""
                }
                return field
            }

        //        @JvmField
        var KNOWLEDGEPOINT_URL: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("KNOWLEDGEPOINT_URL") ?: ""
                }
                return field
            }


        //        @JvmField
        var RESOURCE_URL: String = ""
            get() {
                if (field.isEmpty()) {
                    return MMKV.defaultMMKV()?.decodeString("RESOURCE_URL") ?: ""
                }
                return field
            }


        var COURSE_CLASSID: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("COURSE_CLASSID") ?: ""
            } else field

        var STU_ID: String = ""
            get() = MkUtils.getFromMk(field, "STU_ID")

        var STU_NAME: String = ""
            get() = MkUtils.getFromMk(field, "STU_NAME")


        var SCHOOLID: String = "" //校内ID
            get() = MkUtils.getFromMk(field, "SCHOOLID")


        var KEY: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("KEY") ?: ""
            } else field

        var GLOBAL_GRADE: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("GLOBAL_GRADE") ?: ""
            } else field

        var GLOBAL_GRADE_STU: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("GLOBAL_GRADE_STU") ?: ""
            } else field

        var STAGE_NO: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("STAGE_NO") ?: ""
            } else field

        var COURSE_CLASSNAME: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("COURSE_CLASSNAME") ?: ""
            } else field

        var PROVINCE_ID: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("PROVINCE_ID") ?: ""
            } else field

        var CITY_ID: String = ""
            get() = if (field.isEmpty()) {
                MkUtils.decodeString("CITY_ID") ?: ""
            } else field


        @JvmField
        var INFOLIST: MutableList<BaseInfo>? = null

        @JvmField
        var OBSINFO: OBSInfo? = null

        @JvmField
        var CNTOBSINFO: CoursewareOBSInfo? = null

        @JvmField
        var CoursewareOBSInfo: CoursewareOBSInfo? = null

        val TESTNAME by lazy {
            when (GLOBAL_GRADE) {
                "K6", "K7", "K8", "K9" -> "中考"
                else -> "高考"
            }
        }

        const val endPoint = "https://obs.cn-east-2.myhuaweicloud.com"
        const val SK = "uyFU0hRdcUzfnnAnTlowhZXHIqzHaX3u1rFb1pam"
        const val AK = "Y6H4FPMGPJHBATUSJGHO"
        const val bucketName = "obs-lancooznbk-middle"

        suspend fun executeResponse(response: ZnbkResponse<Any>,
                                    successBlock: suspend CoroutineScope.() -> Unit,
                                    errorBlock: suspend CoroutineScope.() -> Unit) {
            coroutineScope {
                if (response.Code != 1) errorBlock()
                else successBlock()
            }
        }

        suspend fun executeResponse(response: Any, successBlock: suspend CoroutineScope.() -> Unit,
                                    errorBlock: suspend CoroutineScope.() -> Unit) {
            coroutineScope {
                successBlock()
            }
        }
    }
}